package components.utils.observer;

import java.util.Collection;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

public class ComboBoxUpdater extends CollectionObserver {
    
    private JComboBox comboBox;
    private String defaultOption;

    /**
     * Updates the data displayed in the provided Combo Box with the data stored
     * in the collection every time it is notified.
     * 
     * @param collection the collection to update the combo box with.
     * @param comboBox the combo box to be updated.
     * @param defaultOption the option to display in position 0 of the combo box.
     */
    public ComboBoxUpdater(Collection collection, JComboBox comboBox, String defaultOption) {
        super(collection);
        this.comboBox = comboBox;
        this.defaultOption = defaultOption;
    }

    @Override
    public void notifyObserver() {
        comboBox.setModel(new DefaultComboBoxModel(new String[] {defaultOption}));
        for (Object object : collection) {
            comboBox.addItem(object);
        }
    }

}
